package org.tio.sitexxx.service.service.qj;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.model.main.Question;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QuestionService {
    private static Logger log	= LoggerFactory.getLogger(MemberService.class);
    public static final QuestionService me	= new QuestionService();
    final Question dao	= new Question().dao();

    /**
     * 根据   question id 获取question信息
     * @param query
     * @return
     */
    public Question getQuestion(Map<String, Object> query) {
        String sqlid = "question.getQuestion";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        Record record = Db.use(db).findFirst(sqlPara);
        Question question=null;
        if(record!=null){
            question= JSON.parseObject(JSON.toJSONString(record.getColumns()),Question.class);
        }
        return question;
    }

    /**
     * 根据 question id  更新is_public 1为公开   0为不公开  is_paid字段0为未支付  1为已支付
     * can_you_answer  字段什么意思我也不知道，
     * @param id isPublic is_paid
     * @return
     */
    public boolean update_isPublic (Integer id,Boolean isPublic){
        String sqlid = "question.update_isPublic";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,isPublic,id);
        Db.use(db).update(sqlPara.getSql(),isPublic,id);
        return true;
    }

    /**
     * 更新question表 paid 字段
     * @param id
     * @param is_paid
     * @return
     */
    public boolean update_is_paid(Integer id, Boolean is_paid){
        String sqlid = "question.update_is_paid";
        String db = Const.Db.TIO_SITE_MAIN;
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,is_paid,id);
        System.out.println("sql:"+sqlPara.getSql());
        Db.use(db).update(sqlPara.getSql(),is_paid,id);
        return true;
    }
    /**
     * 查询用户所有的已经付过费的问题
     * @param query
     * @return
     */
    public List<Question> getQuestionsByMember(Map<String,Object> query){
        List<Question>resultList=new ArrayList<>(12);
        String sqlid = "question.getQuestionsByMember";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
//        Member list = Member.dao.findFirst(sql, openid);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        List<Record> record = Db.use(db).find(sqlPara);
        if(record!=null){
            record.forEach(record1 -> resultList.add(JSON.parseObject(JSON.toJSONString(record1.getColumns()),Question.class)));
        }
        return resultList;
    }

}
